  import { promote,getCaptial,setCommission } from '@/pages/api/index.js'
  import Clipboard from 'clipboard'
  export default{
		data() {
			return {
        tableData: [],
        dialogVisible: false,
        type: 1,    //佣金的类型1是默认模式，2是二八分模式
        page:{
          pageIndex:1,
          pageSize:10
        },
        dataList:[],         //页面加载展示的数据
        secondList:[],
        count:0,
        params:{
            storeId:0,    //商户id
            type:1,       //类型
            price:[],      //佣金
        },
        inviter:'',          //邀请链接
        href: '',
			}
      },
	  methods:{
		    handleEdit(index, row) {
                 this.dialogVisible = true
                 this.tableData = []
                 this.params.storeId = row.id
                 getCaptial({storeId:row.id}).then(res=>{
                   console.log(res,'res')
                     this.type = res.data.type
                     res.data.data.forEach((item,index) => {
                              this.tableData.push({
                                level:`${item.level}`,
                                value:item.value,
                                defaultValue:item.value + 1,
                                value2:item.value2,
                              })
                     })
                 })

            },
            handleClose(done) {
              this.$confirm('确认关闭？')
                .then(_ => {
                  done();
                })
                .catch(_ => {});
            },
            handleSizeChange(val) {
              console.log(`每页 ${val} 条`);
              this.page.pageSize = val
              this.init()
            },
            handleCurrentChange(val) {
              console.log(`当前页: ${val}`);
              this.page.pageIndex = val
              this.init()
            },
            //初始化
            init(){
              promote(this.params).then(res=>{
                  this.dataList = res.data
                  res.data.data.forEach(item => {
                      if(item.state == 0) {
                         item.state = '待激活'
                      }else if(item.state == 1){
                         item.state = '正常'
                      }else {
                         item.state = '已冻结'
                      }
                  })
                  this.inviter = `${window.location.href}?k=${this.dataList.inviter}`
                  this.href = `${window.location.origin}/login?key=${this.dataList.inviter}`
                  this.secondList = res.data.data
                  this.count = res.data.countUser
              })
            },
            copy(){
               var clipboard = new Clipboard('.tag-read')
               clipboard.on('success',e=>{
                  this.$message.success('复制成功')
                  clipboard.destroy()
               })
               clipboard.on("error",e=>{
                  this.$message.warning('该浏览器不支持复制功能')
                  clipboard.destroy()
               })
            },
            quit(){
              var reg = /^\s*$/g;
              var priceList = {}
              if(this.type == 2){
                  this.tableData.map(item=>{
                    priceList[item.level] = item.value2
                  })
                  this.params.type = this.type
                this.params.price = priceList
                setCommission(this.params).then(res=>{
                    if(res.code == 0){
                        this.$message.success('设置成功')
                        this.dialogVisible = false
                        this.init()
                    }else{
                        this.$message.error(res.msg)
                        return false
                    }
                }).catch(error => {
                    this.$message.error(error)
                })
              }else{
                  this.dialogVisible = false
              }
            }
    },
    created(){
       this.init()
    }
	}
